<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
		//登陆
    public function login()
	{
		$t_agent=M('t_agent');

		if(IS_POST)
		{
			if(trim($_POST['accounts'])=="" || trim($_POST['password'])=="")
			{
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('账号密码不能为空');location.href='".U('Login/login')."'</script>";die;
			}else
			{
				$verify= trim($_POST['verify']);
				$h=$this->check_verify($verify);
				if($h==0)
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('请正确输入验证码');location.href='".U('Login/login')."'</script>";die;
				}
				$accounts=trim($_POST['accounts']);//接受POST传送过来的数据
				$password=trim(md5($_POST['password']));//接受POST传送过来的密码
				$accounts_lenth=strlen($accounts);
				$isagree=trim($_POST['isagree']);
				if($accounts_lenth<11)
				{
					$row=M('t_agent')->where("accounts='$accounts' AND password='$password'")->find();
				}
				elseif($accounts_lenth==11)
				{
					$row=M('t_agent')->where("phone='$accounts' AND password='$password'")->find();
				}
				if($row)
				{
					session('accounts',$row['accounts']);
					session('password',$row['password']);
					session('username',$row['username']);
					session('nickname',$row['nickname']);
					session('referrer',$row['referrer']);
					session('userid',$row['userid']);
					session('phone',$row['phone']);
					session('agencylv',$row['agencylv']);
					session('login_time',$row['login_time']);
					session('gems',$row['gems']);
					$t_login_record=M('t_login_record');
					$t_login_record->login_time=time(); 
					$t_login_record->where('accounts='.$accounts)->save();
					$this->redirect('Index/index');
				}else
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('账号密码错误');location.href='".U('Login/login')."'</script>";die;
				}
			}
		}
			$this->display();	
    }
	
	public function Verify()
	{	
		$Verify =     new \Think\Verify();// 设置验证码字符为纯数字
		$Verify->codeSet = '0123456789';
		$Verify->length   = 4;
		$Verify->entry();
	}

	
	function check_verify($code, $id = '')
	{
		$verify = new \Think\Verify();
		return $verify->check($code, $id);
	}
	public function logint()
	{
		if($_GET['dd']==1)
		{
			session(null);
			echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
			echo "<script type='text/javascript'>alert('退出成功');location.href='".U('Login/login')."'</script>";die;
		}
	}
}

?>
